﻿using System;
using System.Globalization;
using System.Text.RegularExpressions;

using Agile.Service;
using Agile.Service.Rbac;

namespace ConsoleApp
{
    public class TestRbacData : ITestData
    {
        public int DisplayOrder => 2;

        public readonly IMenuElementService menuElementDal = new MenuElementService();
        public readonly IMenuService menuDal = new MenuService(new MenuElementService());
        public readonly IOrganizeService organizeServiceDal = new OrganizeService();
        public readonly IRoleService roleService = new RoleService();
        public readonly IRelevanceService relevanceService = new RelevanceService();
        public readonly UserService userService = new UserService();

        public void Run()
        {
            TestMenuElementData();
            TestOrgData();
            TestRoleData();
            TestUserData();
        }
        private void TestMenuElementData()
        {
            string FlowItem = AddMenu(false, Guid.Empty.ToString(), "FlowItem", "代办事项", "/FlowItem", 0, "fa fa-list-alt");
            AddMenu(true, FlowItem, "FlowWait", "待处理流程", "/flow/flowinstances/wait", 1);
            AddMenu(true, FlowItem, "FlowInstances", "我的流程", "/flow/flowInstances/index", 2);
            AddMenu(true, FlowItem, "FlowDisposed", "已处理流程", "/flow/flowinstances/disposed", 3);
            AddMenu(true, FlowItem, "Comment", "我的评论", "/Sys/Comment/Index", 3);
            AddMenu(true, FlowItem, "Favorite", "我的收藏", "/Sys/Favorite/Index", 8);

            string Notice = AddMenu(false, Guid.Empty.ToString(), "Notice", "通知公告", "/Notice", 1, "fa fa-commenting");
            AddMenu(true, Notice, "Notice", "发布通知", "/Notice", 1);

            string Job = AddMenu(false, Guid.Empty.ToString(), "Job", "招聘管理", "/Job", 2, "fa fa-hand-rock-o");
            AddMenu(true, Job, "Job", "招聘计划", "/Job", 1);
            AddMenu(true, Job, "Job", "简历审批", "/Job", 2);

            string Personnel = AddMenu(false, Guid.Empty.ToString(), "Personnel", "人员管理", "/Personnel", 3, "fa fa-users");
            AddMenu(true, Personnel, "EducationResume", "教育经历", " /Personnel/EducationResume/Index", 1);
            AddMenu(true, Personnel, "TrainResume", "培训经历", "/Personnel/TrainResume/Index", 2);
            AddMenu(true, Personnel, "ProjectResume", "项目经验", "/Personnel/ProjectResume/Index", 3);
            AddMenu(true, Personnel, "WorkResume", "工作经验", "/Personnel/WorkResume/Index", 4);
            AddMenu(true, Personnel, "Socialrelations", "家庭成员", " /Personnel/Socialrelations/Index", 5);

            string PersonnelChange = AddMenu(false, Guid.Empty.ToString(), "ChangeControl", "变动管理", "/ChangeControl", 4, "fa fa-map-signs");
            AddMenu(true, PersonnelChange, "OrgTransfer", "部门调动", "/ChangeControl/OrgTransfer/Index", 1);

            string Attendance = AddMenu(false, Guid.Empty.ToString(), "Attendance", "考勤管理", "/Attendance", 5, "fa fa-tachometer");
            AddMenu(true, Attendance, "leave", "请假销假", "/Attendance/leave/Index", 1);
            AddMenu(true, Attendance, "week", "考勤周报", "/Attendance/week/Index", 2);
            AddMenu(true, Attendance, "month", "考勤月报", "/Attendance/month/Index", 3);
            AddMenu(true, Attendance, "year", "考勤年报", "/Attendance/year/Index", 4);

            string kpi = AddMenu(false, Guid.Empty.ToString(), "kpi", "绩效考核", "/kpi", 6, "fa fa-heartbeat");
            AddMenu(true, kpi, "scheme", "考核方案", "/kpi/scheme/Index", 1);
            AddMenu(true, kpi, "batch", "考核批次", "/kpi/batch/Index", 2);
            AddMenu(true, kpi, "approval", "考核审批", "/kpi/approval/Index", 3);
            AddMenu(true, kpi, "statistics", "考核统计", "/kpi/statistics/Index", 4);

            string Reward = AddMenu(false, Guid.Empty.ToString(), "Reward", "薪酬管理", "/Reward", 7, "fa fa-credit-card");
            AddMenu(true, Reward, "SalaryColumn", "工资公式", "/Reward/SalaryColumn/Index", 1);
            AddMenu(true, Reward, "Salary", "工资表", "/Reward/Salary/Index", 2);

            string FlowDesign = AddMenu(false, Guid.Empty.ToString(), "FlowDesign", "流程中心", "/Flow", 8, "fa fa-openid");
            AddMenu(true, FlowDesign, "Forms", "表单设计", "/flow/DivModel/Index", 1);
            AddMenu(true, FlowDesign, "FlowSchemes", "流程设计", "/flow/flowSchemes/Index", 1);

            string School = AddMenu(false, Guid.Empty.ToString(), "School", "学校信息", "/School", 11, "fa fa-graduation-cap");
            AddMenu(true, School, "SchoolArea", "校区信息", "/School/SchoolArea/Index", 1);
            AddMenu(true, School, "TeachingBuilding", "教学楼", "/School/TeachingBuilding/Index", 2);
            AddMenu(true, School, "FunctionArea", "教学功能区", "/School/FunctionArea/Index", 3);
            AddMenu(true, School, "ClassRoom", "教室信息", "/School/ClassRoom/Index", 4);
            AddMenu(true, School, "Classes", "班级信息", "/School/Classes/Index", 5);
            AddMenu(true, School, "AcadYearTerm", "学年学期", "/School/AcadYearTerm/Index", 6);
            AddMenu(true, School, "Major", "专业信息", "/School/Major/Index", 7);

            string Basics = AddMenu(false, Guid.Empty.ToString(), "Basics", "基础设施", "/Basics", 12, "fa fa-asterisk");
            AddMenu(true, Basics, "user", "用户管理", "/rbac/user/Index", 1);
            AddMenu(true, Basics, "role", "角色管理", "/rbac/role/Index", 2);
            AddMenu(true, Basics, "ItemsData", "数据字典", "/rbac/ItemsData/Index", 3);
            AddMenu(true, Basics, "Duty", "岗位设置", "/rbac/Duty/Index", 4);
            AddMenu(true, Basics, "Organize", "组织机构", "/rbac/organize/Index", 5);
            AddMenu(true, Basics, "menu", "菜单管理", "/rbac/menu/Index", 6);
            AddMenu(true, Basics, "Task", "计划任务", "/Sys/ScheduleTask/Index", 7);
            AddMenu(true, Basics, "SysLog", "系统日志", "/Sys/SysLog/Index", 8);

            string Setting = AddMenu(false, Guid.Empty.ToString(), "Setting", "系统配置", "/Setting", 13, "fa fa-cog");
            AddMenu(true, Setting, "Setting", "全部配置", "/rbac/Setting/Index", 1);
            AddMenu(true, Setting, "GeneralCommon", "通用配置", "/rbac/Setting/GeneralCommon", 2);
            AddMenu(true, Setting, "Attendance", "考勤配置", "/rbac/Setting/Attendance", 3);
            AddMenu(true, Setting, "Job", "招聘配置", "/rbac/Setting/Job", 4);
            AddMenu(true, Setting, "kpi", "考核配置", "/rbac/Setting/kpi", 5);
            AddMenu(true, Setting, "area", "区域管理", "/Sys/Area/Index", 6);
            AddMenu(true, Setting, "application", "应用管理", "/Sys/Application/Index", 7);
            AddMenu(true, Setting, "Friendlink", "友情链接", "/Sys/Friendlink/Index", 8);
            AddMenu(true, Setting, "DatabaseTable", "系统结构", "/Sys/DatabaseTable/Index", 9);
            AddMenu(true, Setting, "Icon", "图标管理", "/Sys/Icon/Index", 10);
            AddMenu(true, Setting, "Healths", "健康检查", "/Sys/Healths/Index",11);
        }
        private string AddMenu(bool but, string pid, string code, string name, string url, int order, string icon = "fa fa-file-text-o")
        {
            var menuId7_2 = GuidHelper.NewSequentialGuid().ToString();
            var model = menuDal.AddOrUpdate(new Menu
            {
                ID = menuId7_2,
                ParentID = pid,
                Code = code.ToLower(),
                Name = name.Trim(),
                Url = url,
                IsSys = true,
                Status = 0,
                Icon = icon,
                OrderBy = order,
                AddTime = DateTime.Now
            });
            string moduleId = model.ID;
            if (but)
            {
                menuElementDal.AddOrUpdate(new MenuElement { ModuleId = moduleId, DomId = "NF-add", Name = "新建", Script = "btn_add()", OrderBy = 1 });
                menuElementDal.AddOrUpdate(new MenuElement { ModuleId = moduleId, DomId = "NF-delete", Name = "删除", Script = "btn_delete()", OrderBy = 2 });
                menuElementDal.AddOrUpdate(new MenuElement { ModuleId = moduleId, DomId = "NF-edit", Name = "修改", Script = "btn_edit()", OrderBy = 3 });
                menuElementDal.AddOrUpdate(new MenuElement { ModuleId = moduleId, DomId = "NF-Details", Name = "查看", Script = "btn_details()", OrderBy = 4 });
            }
            return moduleId;
        }
        private void TestOrgData()
        {
            string root = AddOrganize(Guid.Empty.ToString(), "技术学院", 1);
            string dqjg = AddOrganize(root, "党群机构", 1);
            AddOrganize(dqjg, "院领导", 1);
            AddOrganize(dqjg, "党政办公室", 2);
            AddOrganize(dqjg, "组织人事部", 3);
            AddOrganize(dqjg, "宣传统战部", 4);
            AddOrganize(dqjg, "纪委办", 5);
            AddOrganize(dqjg, "工会", 6);
            AddOrganize(dqjg, "团委", 7);
            string xzjg = AddOrganize(root, "行政机构", 2);
            AddOrganize(xzjg, "教务处", 1);
            AddOrganize(xzjg, "学工处", 2);
            AddOrganize(xzjg, "后勤处", 3);
            AddOrganize(xzjg, "计财处", 4);
            AddOrganize(xzjg, "招就处", 5);
            AddOrganize(xzjg, "保卫处", 6);
            AddOrganize(xzjg, "科研处", 7);
            string jxjg = AddOrganize(root, "教学机构", 3);
            AddOrganize(jxjg, "机电工程分院", 1);
            AddOrganize(jxjg, "艺术分院", 2);
            AddOrganize(jxjg, "学前教育分院", 3);
            AddOrganize(jxjg, "经济管理分院", 4);
            AddOrganize(jxjg, "旅游分院", 5);
            AddOrganize(jxjg, "信息工程分院", 6);
            AddOrganize(jxjg, "汽车工程分院", 7);
            AddOrganize(jxjg, "军体部", 8);
            AddOrganize(jxjg, "思政部", 9);
            AddOrganize(jxjg, "继续教育分院", 10);
            AddOrganize(jxjg, "经管分院", 11);
            string jfjg = AddOrganize(root, "教辅机构", 4);
            AddOrganize(jfjg, "信息中心", 1);
            AddOrganize(jfjg, "图书馆", 2);
            AddOrganize(jfjg, "宣传部", 3);
            AddOrganize(jfjg, "实训产业处", 4);
            AddOrganize(jfjg, "南城留守处", 5);
            AddOrganize(jfjg, "对外交流与合作中心", 6);
            AddOrganize(jfjg, "教学质量管理与评价中心", 7);

        }
        public string AddOrganize(string pid, string name, int order)
        {
            var model = organizeServiceDal.AddOrUpdate(new Organize
            {
                ParentID = pid,
                EndCode = Pinyin.GetInitials(name.Trim()),
                Name = name.Trim(),
                Icon = "",
                OrderBy = order,
                Status = 0,
                TenantId = "",
                AddTime = DateTime.Now,
                IsDelete = 0
            });
            return model.ID;
        }
        private void TestRoleData()
        {
            //常用角色
            AddRole("超级管理员", DicData("角色类型"), 1);
            AddRole("系统管理员", DicData("角色类型"), 1);
            AddRole("系统配置员", DicData("角色类型"), 1);
            AddRole("系统开发人员", DicData("角色类型"), 1);
            AddRole("系统操作员", DicData("角色类型"), 1);
            AddRole("访客人员", DicData("角色类型"), 1);
            AddRole("测试人员", DicData("角色类型"), 1);
            //常用岗位
            AddRole("主席", "", 2);
            AddRole("副书记", "", 2);
            AddRole("馆长", "", 2);
            AddRole("副主任", "", 2);
            AddRole("主任", "", 2);
            AddRole("副处长", "", 2);
            AddRole("处长", "", 2);
            AddRole("副部长", "", 2);
            AddRole("部长", "", 2);
            AddRole("支部书记", "", 2);
            AddRole("副院长", "", 2);
            AddRole("院长", "", 2);
            AddRole("干事", "", 2);
        }
        public void AddRole(string name, string type, int category)
        {
            roleService.AddOrUpdate(new Role
            {
                Name = name.Trim(),
                EnCode = Pinyin.GetInitials(name.Trim()),
                Type = type,
                Category = category,
                AddTime = DateTime.Now
            });
        }
        private void TestUserData()
        {
            for (int i = 0; i < 100; i++)
            {
                string userId = GuidHelper.NewSequentialGuid().ToString();
                string orgId = userService.DBA.ExecuteScalar<string>("SELECT TOP 1 ID FROM rbac_organize WHERE ParentID!='00000000-0000-0000-0000-000000000000' ORDER BY NEWID()");
                string roleId = userService.DBA.ExecuteScalar<string>("SELECT TOP 1 ID FROM dbo.rbac_role WHERE Category=1 ORDER BY NEWID()");
                string[] idCardInfo = IDCardInfoHelper.GetIdCardInfo("362430198508160639");
                var birthDate = idCardInfo[1].ToDateTime();
                userService.AddOrUpdate(new User
                {
                    ID = userId,
                    Account = "luomingui" + i,
                    Name = "罗敏贵" + i,
                    Email = "luomingui@163.com",
                    Password = "123456",
                    IdcardType = "身份证",
                    Idcard = "362430198508160639",
                    Mobile = "13320163492",
                    Sex = 1,
                    Marry = DicData("婚姻状态"),
                    PoliticalID = DicData("政治面貌"),
                    Health = DicData("健康状况"),
                    Faith = DicData("宗教信仰"),
                    Nationality = "中国",
                    BirthDate = birthDate,
                    NativePlace = idCardInfo[0],
                    Address = idCardInfo[0],
                    Zodiac = IDCardInfoHelper.GetZodiac(birthDate),
                    Constellation = IDCardInfoHelper.GetConstellation(birthDate),
                    Zipcode = "000000",
                    NationalID = "汉族",
                });

                relevanceService.AssignOrgUsers(new AssignOrgUsers { OrgId = orgId, UserIds = new string[] { userId }, IsDelete = false });

                relevanceService.AssignRoleUsers(new AssignRoleUsers { RoleId = roleId, UserIds = new string[] { userId }, IsDelete = false });

            }
        }
        public string DicData(string dicName)
        {
            string keyValue = userService.DBA.ExecuteScalar<string>(@"SELECT TOP 1 bid.ID FROM [dbo].[base_itemsdetail] bid 
INNER JOIN [dbo].[base_items] bi ON bid.ItemId=bi.ID WHERE bi.Name = '" + dicName + "' ORDER BY NEWID()");
            return keyValue;
        }
    }
}
